package ru.nsu.fit.client.core.commands;

import ru.nsu.fit.client.core.log_manager.realization.KernelLogManager;
import ru.nsu.fit.client.core.response.KernelResponse;
import ru.nsu.fit.client.core.soap.GetQuestions.GetQuestions;
import ru.nsu.fit.client.core.soap.GetQuestions.GetQuestionsOutput;

public class GetQuestionsCommand implements IClientCommand{
    private final long userSessionId;
    private final String serverUrl;
    private final int questionNumber;
    private final int questionOffset;
    private final int latitude;
    private final int longitude;
    private final int radius;

    public GetQuestionsCommand(final String serverUrl, final long userSessionId, int questionNumber, int questionsOffset, int latitude, int longitude, int radius)
    {
        this.userSessionId = userSessionId;
        this.serverUrl = serverUrl;
        this.questionNumber = questionNumber;
        this.questionOffset = questionsOffset;
        this.latitude = latitude;
        this.longitude = longitude;
        this.radius = radius;
    }

    @Override
    public KernelResponse execute() {
        try
        {
            KernelLogManager.print("Getting questions...");
            GetQuestionsOutput output = GetQuestions.execute(serverUrl, userSessionId, questionNumber, questionOffset, latitude, longitude, radius);

            KernelLogManager.print("Getting questions - got response!");
            KernelResponse response = new KernelResponse(false);
            response.setErrorCode(output.getErrorCode());
            response.setErrorText(output.getErrorMessage());
            response.setData(output.getData());

            return response;
        }
        catch (Exception e)
        {
            KernelLogManager.printf("Getting questions - web service error: %s", e.getMessage());
            KernelResponse response = new KernelResponse(false);
            response.setErrorText(e.getMessage());
            return response;
        }
    }
}
